authApp.value('page','material_apply');
authApp.controller("applyMController",function($scope,$http,$location,$getQueryString,$splice, $cookies){
    $scope.preNum=$getQueryString("preNum");
    $scope.isAddM=true;
    $scope.showEdit = false;
    var requestJson={'fid':$getQueryString("pid")};
    $scope.ids = $cookies.get('ids') == '0' ? '现代化学校' : '优质学校'
    $scope.addM=function(){
        $scope.isAddM=false;
    }
    $scope.cancelAddM=function(){
        $scope.isAddM=true;
        $scope.addFZ="";
        //$scope.addFZNum="";
        $scope.addRequire = "";
        $scope.addST = "0";
    }

    $scope.addFirstZ=function(){
        if($scope.addFZ){
            var rJson={'fid':$getQueryString("pid"),'name':$scope.addFZ,'schoolType':$cookies.get('ids')};

            $http({
                method:'post',
                url:apis.saveObservationPoint,
                data:rJson,
            }).success(function(response){
                var radom=Math.random();
                if(response.code=="000000"){
                    $scope.isAddM=true;
                    $scope.getAML();
                    $scope.addFZ="";
                    $scope.addRequire = "";
                }else{
                    alert(response.errorMsg);
                }
            }).error(function(){
                alert("服务器打瞌睡了，请稍等！");
                $scope.isAddM=true;
            })
        }else{
            alert("请输入测评点");
        }
       

    }
    //编辑
    $scope.edit = function (item) {
        $scope.item = item;
        $scope.showEdit = true;
    }
    $scope.doEdit = function (item) {
        if(!item.s_name){
            alert("请输入测评点");
            return;
        }
       

        var rJson={'schoolType':$cookies.get('ids'),'name':item.s_name,'pid':item.p_id};
        $http({
            method:'post',
            url:apis.updataObservationPoint,
            data:rJson,
        }).success(function(response){
            if(response.code=="000000"){
                alert("编辑成功");
                $scope.showEdit=false;
                $scope.getAML();
                $scope.item="";
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
            $scope.showEdit=false;
        })
    }
    $scope.doCancel = function () {
        $scope.showEdit=false;
    }
    $scope.getAML=function(){
        $http({
            method:'post',
            url:apis.queryAllObservationPoint,
            data:requestJson,
        }).success(function(response){
            if(response.code=="000000"){
                $scope.list=response.data;
                angular.forEach($scope.list,function(data,index,array){
                    data.number = index + 1;
                });
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
        })
    }
    $scope.delCollect=function(pid){
        if(window.confirm("是否确定删除？")){
            var rJson={'pid':pid};
            $http({
                method:'post',
                url:apis.deleteObservationPoint,
                data:rJson,
            }).success(function(response){
                if(response.code=="000000"){
                    $scope.getAML();
                }else{
                    alert(response.errorMsg);
                }
            })
        }
    }
    $scope.turnTo=function(pid){
        location.href="monitorSeeMap.html?pid="+pid;
    }
    $scope.getAML();
})




authApp.directive('observe', function(){
    return {
        restrice: 'EA',
        controller: function($scope, pagerConfig){
            // 共多少条
            $scope.totalItems = 0;
            $scope.page = [];
            // 偏移数
            $scope.offsetPage = 0;
            // 一页多少条
            $scope.itemsPerpage = 0;
            // 一个多少页
            $scope.totalPages = 0;
            $scope.currentPage = 0;

            $scope.$watch('totalItems', function(){
                $scope.totalPages = Math.ceil($scope.totalItems / $scope.itemsPerpage);

                resetPageList();
                if ($scope.page[$scope.currentPage]) {
                    $scope.page[$scope.currentPage].active = true;
                }
            });

            var resetPageList = function(){
                $scope.page = [];

                var last = Math.min(Number($scope.offsetPage) + Number($scope.listSizes), $scope.totalPages);

                for (var i = $scope.offsetPage; i < last; i ++) {
                    $scope.page.push({
                        text: i,
                        indexPage: i,
                        active: false
                    })
                }

            }
            var getOffset = function(index){
                var offset = Math.min(index, $scope.totalPages - $scope.listSizes);
                if (offset <= 0) {
                    offset = 0;
                }
                return offset;
            };
            $scope.selectPage = function(index){
                if (index < 0 || index >= $scope.totalPages) {
                    return;
                }
                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = false;
                }
                $scope.currentPage = index;
                // 如果currentPage 小于 offsetPage 或者 currentPage 大于 offsetPage加listsizes

                if ($scope.currentPage < $scope.offsetPage || $scope.currentPage >= $scope.offsetPage + $scope.page.length) {

                    $scope.offsetPage = getOffset(index)

                    resetPageList();
                }

                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = true;
                }
                $scope.$emit('pagechage', $scope.currentPage);
            };
            $scope.next = function(){
                if ($scope.isLast()) {
                    return;
                }
                $scope.selectPage($scope.currentPage + 1);
            };
            $scope.provie = function(){
                if ($scope.isFirst()) return
                $scope.selectPage($scope.currentPage - 1);
            }
            $scope.first = function(){
                $scope.selectPage(0);
            }
            $scope.last = function(){
                $scope.selectPage($scope.totalPages - 1);
            }
            $scope.isFirst = function(){
                return $scope.currentPage <= 0;
            };
            $scope.isLast = function(){
                return $scope.currentPage >= $scope.totalPages - 1;
            }
            $scope.getText = function(key) {
                return pagerConfig.text[key];
            };


        },
        link: function(scope, ele, attrs){

            scope.itemsPerpage = attrs.itemsperpage || 1;
            scope.listSizes = attrs.listsizes;
            attrs.$observe('totalitems', function(val){
                scope.totalItems = val;
            })
        },
        templateUrl: '../../views/modal/page.html'
    }
}).constant('pagerConfig', {
        text: {
            'first': '首页',
            'provie': '上一页',
            'next': '下一页',
            'last': '尾页',
        }
    }
);

